/*
 * Copyright 2017 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.LoginAO;
import com.lap.scp.admin.core.UserManager;
import com.lap.scp.admin.domain.LoginDO;
import com.lap.scp.admin.domain.UserDO;
import com.lap.scp.admin.utils.ErpEnums.Yn;

/**
 * <pre>
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2017年9月3日 下午2:39:56
 * @since 1.0
 */
@Service("loginAO")
public class DefaultLoginAO extends AbstractAO implements LoginAO {

	@Resource
	private UserManager userManager;

	@Override
	public Resp<UserDO> login(LoginDO loginDO) {
		Resp<UserDO> resp = new Resp<>();
		try {
			UserDO userDO = userManager.queryByUsername(loginDO.getUsername());
			if (userDO == null) {
				resp.putMsg(getMsg("LG00001"));
			} else {
				String encryptedPassWord = DigestUtils.md5Hex(loginDO.getPassword());
				if (!userDO.getPasswd().equals(encryptedPassWord)) {
					resp.putMsg(getMsg("LG00002"));
				} else if (userDO.getStatus() == Yn.No.getValue()) {
					resp.putMsg(getMsg("LG00003"));
				} else {
					resp.setData(userDO);
				}
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(loginDO, e);
		}
		return resp;
	}

}
